package org.benjamin.lending.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
import org.benjamin.lending.domain.entity.GlobalSetting;
import org.benjamin.lending.repository.GlobalSettingRepository;
import org.benjamin.lending.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xywen on 2017/6/9.
 */
@Service
public class SettingServiceImpl implements SettingService{

    private Map<String,String> stringMap;

    @Autowired
    private GlobalSettingRepository globalSettingRepository;

    @Override
    public String getSettingCodeAndKey(String code, String key){
        Map<String,String> deserialize = this.getSettingCode(code);
        return deserialize.get(key);
    }

    @Override
    public Map<String, String> getSettingCode(String code) {
        GlobalSetting globalSetting = this.globalSettingRepository.findOne(code);
        if(globalSetting == null){
            Map<String,String> stringMap = new HashMap<>();
            stringMap.put("getpass","请初始化时间");
            return stringMap;
        }
        return SerializationUtils.deserialize(globalSetting.getTemplate());
    }

    @Override
    public void save(GlobalSetting globalSetting) {
        this.globalSettingRepository.save(globalSetting);
    }
}
